﻿using GEPL.Extensions;
using GEPL.InterfaceLibrary;
using PropertyChanged;
using System;
using System.Runtime.Serialization;
using System.Windows.Controls;

namespace GEPL.ViewModels
{
    /// <summary>
    /// View model for elements in workspace
    /// </summary>
    [Serializable]
    [ImplementPropertyChanged]
    public class Element : ISerializable
    {
        public Element(IElement elementFromToolbox)
        {
            ActualElement = elementFromToolbox.GetType().GetNewInstance<IElement>();
            InitializeUserControlInstance();
        }

        public Element(SerializationInfo info, StreamingContext context)
        {
            ActualElement = (IElement)info.GetValue(nameof(ActualElement), typeof(IElement));
        }

        internal void InitializeUserControlInstance()
        {
            UserControlInstance = ActualElement.UserControlType.GetNewInstance<UserControl>();
            UserControlInstance.DataContext = ActualElement.Data;
        }

        public UserControl UserControlInstance { get; set; }

        public IElement ActualElement { get; set; }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue(nameof(ActualElement), ActualElement, typeof(IElement));
        }
    }
}